﻿package mortal.game.view.common.guide.stepOperater
{
    import com.mui.controls.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import mortal.common.*;
    import mortal.component.gconst.*;
    import mortal.component.window.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.view.common.guide.data.*;
    import mortal.game.view.guide.*;
    import mortal.game.view.wizard.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class AutoGuideWinRectSteper extends AutoGuideStepBase implements IAutoGuideSteper
    {
        protected var _winContentContainer:DisplayObject;
        private var _guideWin:Window;
        private var _rect:Rectangle;
        private var _childs:Array;

        public function AutoGuideWinRectSteper()
        {
            this._rect = new Rectangle();
            return;
        }// end function

        public function guide(param1:AutoGuideData, param2:Window = null) : void
        {
            var _loc_3:Rectangle = null;
            var _loc_6:DisplayObject = null;
            var _loc_7:Point = null;
            var _loc_8:Point = null;
            var _loc_9:Array = null;
            var _loc_10:int = 0;
            var _loc_11:InteractiveObject = null;
            this._rect.x = 0;
            this._rect.y = 0;
            this._rect.width = 1;
            this._rect.height = 1;
            if (param1 == null)
            {
                return;
            }
            _data = param1;
            if (param2 == null)
            {
                param2 = DisplayUtil.getChildByPropety(LayerManager.windowLayer, "winTitleName", param1.guideTypeValue) as Window;
            }
            if (param2 == null)
            {
                param2 = DisplayUtil.getChildByPropety(LayerManager.windowLayer3D, "winTitleName", param1.guideTypeValue) as Window;
            }
            if (param2 == null)
            {
                param2 = DisplayUtil.getChildByPropety(LayerManager.topLayer, "winTitleName", param1.guideTypeValue) as Window;
            }
            if (param2 == null)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.AutoGuideDelDoingRecord, param1));
                return;
            }
            if (_working)
            {
                this.stop();
            }
            this._guideWin = param2;
            this._winContentContainer = param2.contentContainer;
            if (_data.subTypeValue == "3d")
            {
                this._winContentContainer = param2.contentTopOf3DSprite;
            }
            var _loc_4:Boolean = false;
            var _loc_5:* = String(param1.clickChildName).split(";");
            if (_loc_5[0] == "#")
            {
                _loc_6 = DisplayUtil.getChildByPropety(param2, "name", "AutoGuide_Tabar") as DisplayObject;
                if (_loc_6 != null)
                {
                    if (_loc_6 is GTabarNew)
                    {
                        _loc_3 = (_loc_6 as GTabarNew).getTabRectByLabel(_loc_5[1]);
                    }
                    if (_loc_6 is GTabBar)
                    {
                        _loc_3 = (_loc_6 as GTabBar).getTabRectByLabel(_loc_5[1]);
                    }
                    _loc_7 = param2.localToGlobal(GameConst.yellowPoint);
                    _loc_8 = _loc_6.localToGlobal(GameConst.yellowPoint);
                    _loc_3.x = _loc_3.x + (_loc_8.x - _loc_7.x);
                    _loc_3.y = _loc_3.y + (_loc_8.y - _loc_7.y);
                }
            }
            else if (_loc_5[0] == "-" && _loc_5.length > 2)
            {
                _loc_4 = true;
                this._childs = [];
                _loc_3 = new Rectangle();
                _loc_9 = (_loc_5[1] as String).split(",");
                _loc_3.x = _loc_9[0];
                _loc_3.y = _loc_9[1];
                _loc_3.width = _loc_9[2];
                _loc_3.height = _loc_9[3];
                _loc_10 = 2;
                while (_loc_10 < _loc_5.length)
                {
                    
                    _loc_11 = DisplayUtil.getChildByPropety(this._winContentContainer as DisplayObjectContainer, "name", _loc_5[_loc_10]) as InteractiveObject;
                    if (_loc_11 != null)
                    {
                        _loc_11.addEventListener(MouseEvent.CLICK, this.clickWinChildHandler);
                        this._childs.push(_loc_11);
                        _loc_11.filters = [FilterConst.guideFilter];
                    }
                    _loc_10++;
                }
            }
            else
            {
                _loc_3 = new Rectangle();
                _loc_3.x = int(_loc_5[0]);
                _loc_3.y = int(_loc_5[1]);
                _loc_3.width = int(_loc_5[2]);
                _loc_3.height = int(_loc_5[3]);
            }
            if (param1.needMask)
            {
                showMaskByRect(param2.highestContentContainer, _loc_3);
            }
            if (param1.needArrow)
            {
                showArrowByRect(param2.highestContentContainer, param1, _loc_3);
            }
            if (param1.needLightMask)
            {
                showLightMaskByRect(param2.highestContentContainer, _loc_3);
            }
            if (param1.filterName == AutoGuideData.filterRectBitmapMask)
            {
                showRectBitmapLightMask(param2.highestContentContainer, _loc_3);
            }
            if (!_loc_4)
            {
                if (param1.clickType == AutoGuideClickType.Click)
                {
                    this._winContentContainer.addEventListener(MouseEvent.CLICK, this.clickWinChildHandler, true);
                }
                else
                {
                    this._winContentContainer.addEventListener(MouseEvent.DOUBLE_CLICK, this.clickWinChildHandler, true);
                }
            }
            this._guideWin.addEventListener(WindowEvent.CLOSE, this.winCloseHandler);
            if (_loc_3 != null)
            {
                this._rect = _loc_3.clone();
            }
            super.setData(param1, this._guideWin.highestContentContainer);
            return;
        }// end function

        override protected function guideTimerOut() : void
        {
            if (this._guideWin != null)
            {
                this._guideWin.removeEventListener(WindowEvent.CLOSE, this.winCloseHandler);
                if (this._guideWin is IView)
                {
                    (this._guideWin as IView).hide();
                }
            }
            if (this._winContentContainer != null)
            {
                this._winContentContainer.removeEventListener(MouseEvent.CLICK, this.clickWinChildHandler, true);
                this._winContentContainer.removeEventListener(MouseEvent.DOUBLE_CLICK, this.clickWinChildHandler, true);
            }
            super.guideTimerOut();
            this._guideWin = null;
            return;
        }// end function

        private function winCloseHandler(event:WindowEvent) : void
        {
            this.stop();
            if (GuideController.shouldResumeToTaskAfterGuide() && _data.filterName != "NoMainEndType")
            {
                GameController.guide.resumeToTask();
            }
            return;
        }// end function

        protected function clickWinChildHandler(event:MouseEvent) : void
        {
            if (this._rect != null && this._winContentContainer != null)
            {
                if (!this._rect.contains(this._winContentContainer.mouseX, this._winContentContainer.mouseY))
                {
                    return;
                }
            }
            this.stop(_data);
            return;
        }// end function

        override public function stop(param1:AutoGuideData = null) : void
        {
            var _loc_2:int = 0;
            var _loc_3:InteractiveObject = null;
            super.stop(param1);
            if (this._childs != null && this._childs.length > 0)
            {
                _loc_2 = 0;
                while (_loc_2 < this._childs.length)
                {
                    
                    _loc_3 = this._childs[_loc_2];
                    _loc_3.removeEventListener(MouseEvent.CLICK, this.clickWinChildHandler);
                    _loc_3.filters = [];
                    _loc_2++;
                }
                _child = null;
            }
            if (this._guideWin != null)
            {
                this._guideWin.removeEventListener(WindowEvent.CLOSE, this.winCloseHandler);
            }
            if (this._winContentContainer != null)
            {
                this._winContentContainer.removeEventListener(MouseEvent.CLICK, this.clickWinChildHandler, true);
                this._winContentContainer.removeEventListener(MouseEvent.DOUBLE_CLICK, this.clickWinChildHandler, true);
            }
            if (param1 != null)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.AutoGuideStepEnd, param1));
            }
            return;
        }// end function

    }
}
